package com.xu.modules.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xu.common.domain.PageQuery;
import com.xu.common.domain.TableDataInfo;
import com.xu.common.enums.ArticleNumTypeEnum;
import com.xu.common.enums.StationNumTypeEnum;
import com.xu.modules.entity.ArticleEntity;
import com.xu.modules.mapper.ArticleMapper;
import com.xu.modules.service.ArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xu.modules.service.StationService;
import com.xu.modules.service.TagService;
import com.xu.modules.service.bo.ArticleBo;
import com.xu.modules.service.vo.ArticleVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 文章 服务实现类
 * </p>
 *
 * @author xu
 * @since 2022-07-26
 */
@Service
@RequiredArgsConstructor
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, ArticleEntity> implements ArticleService {
    private final TagService tagService;
    private final ArticleMapper articleMapper;
    private final StationService stationService;

    @Override
    public void addArticleNum(Long articleId, ArticleNumTypeEnum articleNumTypeEnum) {
        LambdaQueryWrapper<ArticleEntity> lqw = Wrappers.lambdaQuery();
        lqw.eq(ArticleEntity::getArticleId,articleId);
        ArticleEntity articleEntity = articleMapper.selectOne(lqw);
        if(ArticleNumTypeEnum.VIEW.equals(articleNumTypeEnum)){
            int num = articleEntity.getViewNum() + 1;
            articleEntity.setViewNum(num);
        }
        if(ArticleNumTypeEnum.COMMENT.equals(articleNumTypeEnum)){
            int num = articleEntity.getCommentNum() + 1;
            articleEntity.setCommentNum(num);
        }
        if(ArticleNumTypeEnum.GOOD.equals(articleNumTypeEnum)){
            int num = articleEntity.getGoodNum() + 1;
            articleEntity.setGoodNum(num);
        }
        articleMapper.updateById(articleEntity);
    }

    @Override
    public ArticleVo detail(Long articleId) {
        LambdaQueryWrapper<ArticleEntity> lqw = Wrappers.lambdaQuery();
        lqw.eq(ArticleEntity::getArticleId,articleId);
        ArticleEntity articleEntity = articleMapper.selectOne(lqw);

        // 先增加观看人次
        int num = articleEntity.getViewNum() + 1;
        articleEntity.setViewNum(num);
        articleMapper.updateById(articleEntity);

        ArticleVo articleVo = BeanUtil.copyProperties(articleEntity, ArticleVo.class);
        List<String> tagList = tagService.listByArticleId(articleId);
        articleVo.setArticleTagList(tagList);

        return articleVo;
    }

    @Override
    public TableDataInfo<ArticleVo> listPage(ArticleBo articleBo, PageQuery pageQuery) {
        LambdaQueryWrapper<ArticleEntity> lqw = Wrappers.lambdaQuery();
        lqw.eq(articleBo.getArticleType() != null,ArticleEntity::getArticleType,articleBo.getArticleType());

        Page<ArticleEntity> articleEntityPage = articleMapper.selectPage(pageQuery.build(), lqw);

        return TableDataInfo.build(articleEntityPage,ArticleVo.class);
    }

    @Override
    public void add(ArticleBo articleBo) {
        LambdaQueryWrapper<ArticleEntity> lqw = Wrappers.lambdaQuery();
        lqw.last("order by article_id desc LIMIT 1");

        // 上一个
        ArticleEntity preArticleEntity = articleMapper.selectOne(lqw);

        // 设置最初值
        ArticleEntity articleEntity = BeanUtil.copyProperties(articleBo, ArticleEntity.class);
        // 默认不置顶
        articleEntity.setIsTop(false);
        // 如果不为空 就有上一个
        if(preArticleEntity != null){
            articleEntity.setPreArticleId(preArticleEntity.getArticleId());
            articleEntity.setPreArticleTitle(preArticleEntity.getTitle());
        }
        // 先插入 获取主键ID
        articleMapper.insert(articleEntity);
        if(preArticleEntity != null){
            preArticleEntity.setNextArticleId(articleEntity.getArticleId());
            preArticleEntity.setNextArticleTitle(articleEntity.getTitle());
            articleMapper.updateById(preArticleEntity);
        }
        // 设置文章标签
        tagService.addByArticleId(articleBo.getArticleTagList(),articleEntity.getArticleId());
        stationService.changeStationNum(StationNumTypeEnum.ARTICLE);
    }
}
